from time import sleep

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from Util.Logger import Log
from Util.ParserConfigFile import ParserConfigFile
from Util.util import find_element


class loginPage:

    def __init__(self,wd):
        self.wd = wd
        self.parse = ParserConfigFile()
        self.loginLocation = self.parse.getSection('163main_login')
        self.log = Log()
    def switch_frame(self):
        '''
        切换 iframe
        :return:
        '''
        try:
            self.log.info('开始切换iframe')
            #i_frame = find_element(self.wd,'tag name','iframe')
            location_type,expression = self.loginLocation['login_page.frame'].split(':')
            i_frame = find_element(self.wd,location_type,expression)
            self.wd.switch_to.frame(i_frame)
        except Exception as e:
            print(e)

    def input_username(self,username):
        '''
        用户名
        :param username:
        :return:
        '''
        try:
            self.log.info('开始输入用户名')
            location_type, expression = self.loginLocation['login_page.username'].split(':')
            find_element(self.wd,location_type,expression).send_keys(username)
        except Exception as e:
            print(e)

    def input_password(self,password):
        '''
        输入密码
        :param password:
        :return:
        '''
        try:
            self.log.info('开始输入密码')
            location_type, expression = self.loginLocation['login_page.password'].split(':')
            find_element(self.wd, location_type, expression).send_keys(password)
        except Exception as e:
            print(e)

    def login_click(self):
        '''
        点击登录按钮
        :return:
        '''
        try:
            self.log.info('点击登录按钮')
            location_type, expression = self.loginLocation['login_page.click'].split(':')
            find_element(self.wd, location_type, expression).click()
        except Exception as e:
            print(e)
        self.log.info('Case Action Success')

if __name__ == '__main__':
    chrome_Path = 'E:\RF\chromedriver.exe'
    options = Options()
    options.add_argument('--start-maximized')
    wd = webdriver.Chrome(executable_path=chrome_Path, options=options)
    wd.get('https://mail.163.com/')

    login_page = loginPage(wd)
    # print('1')
    login_page.switch_frame()
    # print('2')
    login_page.input_username('13037521553')
    # sleep(2)
    login_page.input_password('Ruru970929.')
    login_page.login_click()
